import mountElement from './mountElement'
import updateElementNode from './updateElementNode'

export default function createDOMElement(virtualDOM) {
    let newElement = null;

    if(virtualDOM.type === 'text') {
        newElement = document.createTextNode(virtualDOM.props.textContent)
    } else {
        newElement = document.createElement(virtualDOM.type)
        updateElementNode(newElement, virtualDOM)
    }

    newElement._virtualDOM = virtualDOM

    virtualDOM.children.forEach(child => {
        // 不确定子元素是 nativeElement 还是 Component
        mountElement(child, newElement)
    })

    if(virtualDOM.props && virtualDOM.props.ref) {
        virtualDOM.props.ref(newElement)
    }

    return newElement
}